package vooga.xml.handlers;

import vooga.xml.XMLException;


public class PrintHandler extends vooga.xml.XMLHandler
{
    public Object handleNode (org.w3c.dom.Node node, Object context) throws XMLException
    {
        Counter level = (Counter)context;
        String result = tab(level) + node.getNodeName() + " ";

        java.util.List attributes = getAttributes(node);
        for (int k = 0; k < attributes.size(); k++)
        {
            org.w3c.dom.Node current = (org.w3c.dom.Node)attributes.get(k);
            result += tab(level) + current.getNodeName() + "=" + current.getNodeValue() + " ";
        }
        result += "\n";

        level.increment();
        java.util.List children = getSubNodes(node);
        for (int k = 0; k < children.size(); k++)
        {
            org.w3c.dom.Node current = (org.w3c.dom.Node)children.get(k);
            result += getDocument().getHandler(current.getNodeName()).handleNode(current, context);
        }
        level.decrement();

        return result;
    }


    private String tab (Counter level)
    {
        String result = "";
        for (int k = level.getValue(); k > 0; k--)
        {
            result += " ";
        }
        return result;
    }
}
